package com.ysw.springbootproject.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 月上微
 * @date 2025/1/6
 * @since 17:02
 */
public class NumberUtil {

    private NumberUtil() {

    }

    private static final Pattern PHONE_REGEX = Pattern.compile("^((13[0-9]|(14[5,7])|(15[^4,\\D])|(17[0-8])|(18[0-9]))\\d{8}$)");

    /**
     * 判断是否为 11 位电话号码
     *
     * @param phone String
     * @return boolean
     */
    private static boolean isPhone(String phone) {
        Matcher matcher = PHONE_REGEX.matcher(phone);
        return matcher.matches();
    }

    /**
     * 生成指定长度的随机数
     *
     * @param length int
     * @return int
     */
    public static int genRandomNum(int length) {
        int num = 1;
        double random = Math.random();
        if (random < 0.1) {
            random = random + 0.1;
        }
        for (int i = 0; i < length; i++) {
            num = num * 10;
        }
        return (int) (random * num);
    }

    /**
     * 生成订单号
     *
     * @return String
     */
    public static String genOrderNo() {
        StringBuffer sb = new StringBuffer(String.valueOf(System.currentTimeMillis()));
        int num = genRandomNum(4);
        sb.append(num);
        return sb.toString();
    }

    public static String formatMoney2Str(Double money) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        return df.format(money);
    }

    public static String formatMoney2Str(float money) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        return df.format(money);
    }
}
